<template>
  <div class="home-container">
    <!-- 推荐博客 -->
    <section class="recommended-blogs">
      <h2>我发布的博客</h2>
      <ul>
        <li v-for="blog in Blogs" :key="blog.bid" @click="navigateToDetail(blog.bid)">
          <h3>{{ blog.title }}</h3>
          <p class="blog-content">{{ blog.content }}</p>
          <div class="blog-meta">
            <span>作者: {{ this.$store.state.user.userinfo.nickname }}</span>
            <span>点赞数: {{ blog.countLike }}</span>
            <span>评论数: {{ blog.countcmmt }}</span>
          </div>
          <div class="blog-tags" v-if="blog.label && blog.label.length > 0">
            <span class="tag" v-for="tag in blog.label" :key="tag">{{ tag }}</span>
          </div>
          <button class="delete-button" @click.stop="deleteBlog(blog.bid)">×</button>
          <button class="edit-button" @click.stop="openEditDialog(blog)">编辑</button>
          <div class="divider"></div>
        </li>
      </ul>
    </section>

    <!-- 编辑博客对话框 -->
    <div v-if="isEditing" class="modal">
      <div class="modal-content">
        <span @click="closeEditDialog">&times;</span>
        <h2>编辑博客</h2>
        <input v-model="currentBlog.title" type="text" class="blog-title-input">
        <textarea v-model="currentBlog.content" class="blog-content-input"></textarea>
        <button @click="saveBlogChanges">保存</button>
      </div>
    </div>
  </div>
</template>

<script>
import api from '@/api/index';
import {useRouter} from 'vue-router';

export default {
  name: 'Home',
  data() {
    return {
      // 当前用户
      user: this.$store.state.user,
      Blogs: [],
      isEditing: false,
      currentBlog: {}
    };
  },
  setup() {
    const router = useRouter();
    return {router};
  },
  mounted() {
    console.log("全局用户信息==", this.$store.state.user);
    this.fetchRecommendedBlogs();
  },
  methods: {
    fetchRecommendedBlogs() {
      // alert(this.user);
      api.get("/api/getBlogbyuid", {params: {"uid": this.user.uid}}).then(res => {
        console.log(res);
        this.Blogs = res.serverData;
      });
    },
    navigateToDetail(blogId) {
      this.router.push(`/blog/${blogId}`);
    },
    deleteBlog(bid) {
      // 发送删除请求
      api.get(`/api/delBlogbybid/` + bid).then(res => {
        console.log(res);
        if (res.code === 200) {
          // 删除成功，刷新博客列表
          this.fetchRecommendedBlogs();
        }
      });
    },
    openEditDialog(blog) {
      console.log(blog)
      // 复制博客对象以避免直接修改原数据
      this.currentBlog = {...blog};
      this.isEditing = true;


    },
    closeEditDialog() {
      this.isEditing = false;
    },
    saveBlogChanges() {
      api.put("api/updateBlogbybid", {
        "bid": this.currentBlog.bid,
        "content": this.currentBlog.content,
        "title": this.currentBlog.title
      }).then(res => {
        if (res.code === 200) {
          // 修改成功，刷新博客列表
          this.fetchRecommendedBlogs();
          this.currentBlog = {};
          //  关闭对话框
          this.isEditing = false;
        }

      })
    }
  }
};
</script>

<style scoped>
.author-avatar {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  margin-right: 5px;
  vertical-align: middle;
}

.blog-title {
  font-weight: bold;
  cursor: pointer;
  text-align: left;
  display: block;
  margin-bottom: 10px;
}

.home-container {
  padding: 20px;
  max-width: 1200px; /* 增加最大宽度 */
  margin: 0 auto; /* 居中显示 */
}

section {
  margin-bottom: 40px;
}

h2 {
  font-size: 24px;
  margin-bottom: 20px;
}

ul {
  list-style: none;
  padding: 0;
}

li {
  margin-bottom: 20px;
  padding: 15px;
  background-color: #f9f9f9;
  border-radius: 5px;
  position: relative; /* 为了定位删除按钮 */
}

.blog-content {
  max-height: 100px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

.blog-meta {
  font-size: 14px;
  color: #666;
  margin-top: 10px;
}

.blog-meta span {
  margin-right: 15px;
}

.blog-tags {
  margin-top: 10px;
}

.tag {
  display: inline-block;
  background-color: #e1f5fe;
  color: #007bff;
  padding: 2px 8px;
  border-radius: 16px;
  font-size: 12px;
  margin-right: 5px;
  margin-bottom: 5px;
}

.divider {
  height: 1px;
  background-color: #ddd;
  margin: 15px 0;
}

.delete-button {
  position: absolute;
  top: 10px;
  right: 10px;
  background-color: transparent;
  border: none;
  font-size: 20px;
  cursor: pointer;
}

.edit-button {
  position: absolute;
  top: 10px;
  right: 30px;
  background-color: transparent;
  border: none;
  font-size: 16px;
  cursor: pointer;
}

.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-content {
  background-color: #fff;
  padding: 20px;
  border-radius: 5px;
  width: 500px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
  position: relative;
}

.modal-content span {
  position: absolute;
  top: 10px;
  right: 10px;
  font-size: 20px;
  cursor: pointer;
}

.blog-title-input {
  width: 100%;
  padding: 8px;
  margin-bottom: 10px;
  font-size: 16px;
}

.blog-content-input {
  width: 100%;
  height: 100px;
  padding: 8px;
  margin-bottom: 10px;
  font-size: 14px;
}
</style>